﻿//using System;
//using System.Collections.Generic;
//using System.Text;
//using DCRF.Common.Core;
//using DCRF.Common.Primitive;
//using DCRF.Common.Interface;
//using System.IO;
//using DCRF.Common.Kernel.Core;
//using System.Windows.Forms;
//using DCRF.BaseBlocks.Commander.Console;
//using DCRF.BaseBlocks.Commander.Console.Nodes;
//using DCRF.Common.Attributes;

//namespace DCRF.BaseBlocks.Commander.Console
//{
//    [BlockId("BlockTreeBlock")]
//    [BlockConnector("Container", typeof(Control))]
//    [BlockConnector("AddToGlobalState", null)]
//    public class BlockTreeBlock: BlockBase
//    {
//        private IBlockBroker myBroker = null;
//        private TreeView myTree = null;
//        //private BlockWebManager myManager = null;
//        private BlockWebNode webNode = null;

//        private ContextMenu ctx = null;
//        private TreeNode activeNode = null;

//        //public override BlockInfo BlockInfo
//        //{
//        //    get
//        //    {
//        //        BlockInfo result = new BlockInfo();

//        //        //result.BlockID = CID.New("BlockTreeBlock");
//        //        return result;
//        //    }
//        //}

//        public BlockTreeBlock(Guid id, IContainerBlockWeb parent) : base(id, parent) { }

//        public override void InitBlock()
//        {
//            myBroker = new SimpleBlockBroker();
//            RepositoryOptions opt = new RepositoryOptions();
//            opt.Folder = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "blocks");
//            myBroker.SetupBroker(opt);

//            innerWeb = new BlockWeb("ROOTWEB", myBroker);

//            ctx = new ContextMenu();

//            MenuItem mnuAddToState = new MenuItem("Add to GlobalState", new EventHandler(onAddStateClick));
//            MenuItem mnuRefresh = new MenuItem("Refresh", new EventHandler(onRefreshClick));
//            MenuItem mnuCopy = new MenuItem("Copy ID to Clipboard", new EventHandler(onCopyId));

//            mnuAddToState.Tag = "ADD";
//            mnuRefresh.Tag = "REF";
//            mnuCopy.Tag = "COP";

//            ctx.MenuItems.Add(mnuAddToState);
//            ctx.MenuItems.Add(mnuRefresh);
//            ctx.MenuItems.Add(mnuCopy);
//        }

//        [BlockService]
//        public void BindToUI()
//        {
//            Control container = Connectors["Container"].Value as Control;
//            myTree = new TreeView();

//            container.Invoke(new MethodInvoker(delegate()
//            {
//                container.Controls.Add(myTree);
//                myTree.Dock = DockStyle.Fill;

//                TreeNode root = new TreeNode();
//                myTree.Nodes.Add(root);
//                webNode = new BlockWebNode(root, innerWeb);

//                myTree.ContextMenu = ctx;
//                myTree.MouseClick += new MouseEventHandler(myTree_Click);
//            }));
//        }

//        void myTree_Click(object sender, MouseEventArgs e)
//        {
//            if (e.Button == MouseButtons.Right)
//            {
//                activeNode = myTree.GetNodeAt(e.X, e.Y);

//                if (activeNode == null) return;

//                INode node = activeNode.Tag as INode;

//                List<string> cmdList = node.Commands;

//                //hide menu items that are not supported by this node
//                foreach (MenuItem mnu in ctx.MenuItems)
//                {
//                    mnu.Visible = (cmdList.Contains((string)mnu.Tag));
//                }
//            }
//        }

//        private int counter = 0;
//        private void onAddStateClick(object sender, EventArgs args)
//        {
//            string key = "Item" + counter.ToString();
//            counter++;

//            object nodeObject = (activeNode.Tag as INode).ProcessRequest("ADD", counter, this["AddToGlobalState"]);
//        }

//        private void onRefreshClick(object sender, EventArgs args)
//        {
//            (activeNode.Tag as INode).ProcessRequest("REF");
//        }

//        private void onCopyId(object sender, EventArgs args)
//        {
//            (activeNode.Tag as INode).ProcessRequest("COP");
//        }
//    }
//}
